import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { RecordWay, Unit, SetsType } from 'src/types/motion';

// 单个动作编排配置
@Schema({
  _id: false,
})
export class PlanMotion {
  @Prop({
    type: Types.ObjectId,
    ref: 'Motion',
    required: true,
    index: true,
  })
  motionId: Types.ObjectId;

  @Prop({
    type: String,
    required: true,
  })
  uuid: string;

  @Prop({ type: Number, enum: RecordWay, default: RecordWay.sets })
  recordWay?: RecordWay;

  @Prop({ type: Number, enum: SetsType, default: SetsType.reps })
  setsType?: SetsType;

  @Prop({
    type: Number,
    enum: Unit,
    default: Unit.kg,
  })
  unit?: Unit;

  @Prop({
    type: Array<[Number, Number]>,
    default: [],
  })
  sets?: [number, number][];

  @Prop({
    type: Number,
    default: 0,
  })
  innerRest?: number;

  @Prop({
    type: Number,
    default: 0,
  })
  outerRest?: number;
}

@Schema({
  timestamps: true,
  collection: 'plans',
  versionKey: false,
})
export class Plan extends Document {
  @Prop({
    type: String,
    required: true,
    maxlength: 20,
    index: true,
  })
  name: string; // 模板名称

  @Prop({
    type: [PlanMotion],
    required: true,
  })
  motions: PlanMotion[];

  @Prop({
    type: Types.ObjectId,
    ref: 'User',
    required: true,
    index: true,
    select: false,
  })
  userId: Types.ObjectId;
}

export const PlanSchema = SchemaFactory.createForClass(Plan);

// 添加复合索引
PlanSchema.index({ userId: 1, name: 1 }, { unique: true });
PlanSchema.index({ 'motions.motionId': 1 });
